package com.baizhi.service.impl;

import com.baizhi.annotation.DeleteAnnotation;
import com.baizhi.annotation.InsertAnnotation;
import com.baizhi.dao.AdminDao;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired(required = false)
    private AdminDao adminDao;
    @Override
    public Admin login(Admin admin) {

        return adminDao.selectOne(admin);
    }

    @Override
    @InsertAnnotation
    public Map selectAll(int page, int limit, String userName) {
        int offset=(page-1)*limit;
        List<Admin> list = adminDao.selectAll(offset, limit);
        int count=adminDao.selectCount(userName);
        Map map=new HashMap();
        map.put("data",list);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @Override
    public Admin selectById(int id) {
        return adminDao.selectById(id);
    }

    @Override
    @DeleteAnnotation
    public Map insert(Admin admin) {
        Map map=new HashMap();
        int count=adminDao.insert(admin);
        if(count>0){
            map.put("isInsert","添加成功");
        }else{
            map.put("isInsert","添加失败");
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map delete(int id) {
        int count=adminDao.delete(id);
        Map map=new HashMap();
        if(count>0){
            map.put("isDelete","删除成功");
        }else{
            map.put("isDelete","删除失败");
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map update(Admin admin) {
        int count=adminDao.update(admin);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","更新成功");
        }else{
            map.put("isUpdate","更新失败");
        }
        return map;
    }

    @Override
    public Set<String> selectRole(String userName) {
        return adminDao.selectRole(userName);
    }

    @Override
    public Set<String> selectResource(String userName) {
        return adminDao.selectResource(userName);
    }
}
